package com.woniuxy.bookmall.service;

import com.woniuxy.bookmall.entity.TBooktype;
import com.woniuxy.bookmall.mapper.TBooktypeMapper;
import com.woniuxy.bookmall.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BookTypeService {

    @Autowired
    RedisUtils redisUtils ;

    @Autowired
    TBooktypeMapper booktypeMapper ;

    public List<TBooktype> findAll(){
        List<TBooktype> type = (List<TBooktype>) redisUtils.get("WONIU_BOOK_TYPE");
        log.info("Redis查询的图书类型数据:{}",type);
        if(type != null) {
            return type ;
        }else  {
            // 处理一个 缓存击穿问题
            synchronized (BookTypeService.class){
                //在此 查询缓存
                type =(List<TBooktype>)redisUtils.get("WONIU_BOOK_TYPE");
                if (type == null) {
                    List<TBooktype> lists = booktypeMapper.selectAll();
                    redisUtils.set("WONIU_BOOK_TYPE",lists,15*60);
                    return lists ;
                }else {
                    return type ;
                }
            }
        }
    }
}
